﻿using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using G.Runtime;
using GameLibrary.Model;
using Microsoft.VisualBasic.PowerPacks;
using GameLibrary.Model.Core;

namespace GameDesigner.Source.Widgets
{
    class PlayerPanel
    {
        private readonly GameDef _GameDef;
        private readonly Game _Game;
        private readonly SimpleShape _SimpleShape;
        private readonly Dictionary<string, Updatable> _Widgets = 
            new Dictionary<string, Updatable>();
        private readonly List<GameValue> _BoundVariables = new List<GameValue>();
        private readonly GamePlayer _GamePlayer;

        public PlayerPanel(GamePlayer pGamePlayer, SimpleShape pSimpleShape)
        {
            _GameDef = pGamePlayer.Game.GameDef;
            _Game = pGamePlayer.Game;
            _GamePlayer = pGamePlayer;
            _SimpleShape = pSimpleShape;
            _GamePlayer.GameValues.ChangedEvent += OnChangedEvent;
            _SimpleShape.BackColor = pGamePlayer.Def.Color;
            
            LabelWidget w = new LabelWidget(_SimpleShape, _GamePlayer.Name);
            _Widgets.Add("player", w);
        
            Sync();
            DisplayValues();
        }

        void OnChangedEvent(GameValues pGameValues)
        {
            Sync();
            DisplayValues();
        }
        
        void OnChangedEvent(GameValue var)
        {
            Sync();
            DisplayValues();
            _SimpleShape.Refresh();
        }
        
        private void Sync()
        {
            foreach (GameValue v in _GamePlayer.GameValues)
            {
                if (!_BoundVariables.Contains(v) && v.Def.Display)
                {
                    _BoundVariables.Add(v);
                    v.ChangedEvent += OnChangedEvent;
                    {
                        BoundVariableWidget w = new BoundVariableWidget(v, _SimpleShape);
                        _Widgets.Add(v.Def.Name, w);
                    }
                }
            }
            
            List<GameValue> remove = new List<GameValue>();
            foreach (GameValue v in _BoundVariables)
            {
                if (!_GamePlayer.GameValues.Contains(v))
                {
                    remove.Add(v);
                }
            }
            
            RemoveValues(remove);
        }
        
        private void RemoveValues(List<GameValue> remove)
        {
            List<GameValue> temp = new List<GameValue>(remove);
            
            foreach (GameValue v in temp)
            {
                v.ChangedEvent -= OnChangedEvent;
                _Widgets.Remove(v.Def.Name);
                _BoundVariables.Remove(v);
            }
        }
        
        private void DisplayValues()
        {
            int y = 5;
            foreach (Widget w in _Widgets.Values)
            {
                w.Point = new Point(5, y);
                y += 20;
            }
        }

        public void OnPaint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen pen = new Pen(Color.LightGray, 1);
            foreach (Widget w in _Widgets.Values)
            {
                w.Update(g);
            }
            pen.Dispose();
        }
    }
}
